<?php
/**
 * Controller_Ajax_Notify
 *
 * Класс Controller_Ajax_Notify 
 */
class Controller_Ajax_Notify extends Controller_Ajax {

	/**
	 * (non-PHPdoc)
	 *
	 */
	public function actionSwitch() {
	  Service_Debug::setDebug();
		switch ($this->arrRequest[2]) {
			case 'want-call':
				$this->actionWantCall();
				break;
			default: $this->setError(404);
 		}
	}

	
	/**
	 * Запрос клиента на звонок
	 *
	 */
	public function actionWantCall () {
		Service_Debug::setDebug();
		$objDB = Service_Database::instance();
		$phone = isset($_POST['phone']) ? Service_Escape::absolutelyEscapeString($_POST['phone']) : '';
		$time = isset($_POST['time']) ? Service_Escape::absolutelyEscapeString($_POST['time']) : '';
		
		if (empty($phone) || empty($time)) return $this->makeStandardError('Не получены данные');
		if(!preg_match('#^[0-9]{4,16}$#', $phone) || intval($time) > 20 || intval($time) < 8) return $this->makeStandardError('Неверные данные');
		
		$arFieldValues = array('phone' => $phone, 'time' => $time, 'ID_user' => User_User::getId() ? User_User::getId() : null);
		
		if($objDB->insert('want_call_notify', $arFieldValues)) {
			unset($arFieldValues['ID_user']);
			$objNotification = new User_Notification();
			$objNotification->callOrder($arFieldValues);
			$this->arrReturn = array('result' => 'OK');
		}
	}
}
?>